#ifndef _iap_h_
#define _iap_h_

// iap.h
//
// iap file delegate (our own file format)


#include "delegates/io.h"

namespace simple{

    //
    // IAP
    //
    // IAP storage delegate. Being our own file format we will keep it
    // simple, and maintain all the necessary information


    class IAP : public ImageIOBase
    {
    public:

        static IAP& gOnly ();

        virtual DelegateInfo info (const std::string& filename);
        // Given data from the header of an image, return information
        // about this image, or indicate that nothing is known.

        virtual Image<RGB>  readRGB  (const std::string& filename);
        virtual Image<Pel8> readPel8 (const std::string& filename);

        bool write (const std::string& filename,
                    const RectImageStorage& pixels,
                    const DelegateParams& params = sNoParams);


        struct IAPFileHeader
        {
            char signature[8];     // File signature 'IAP     '
            long version;          // File version (ignored for now)
            long nImages;          // Number of images stored (normally 1)
        };


        struct IAPImageHeader
        {
            long    x0;        // Coordinates of upper-left corner
            long    y0;
            long    width;     // Image width
            long    height;    // Image height
            long    channels;  // Number of channels (1, 3)
            long    type;      // Type (see eIAPFileType below)
            long    depth;     // Bits per pixel per channel (8, 32)
            long    format;    // Format (see eIAPFileFormat below)
        };


        enum eIAPFileType
        {
            eMono = 0,             // Monochrome image
            eRGB  = 1,           // RGB image
        };

        enum eIAPFileFormat
        {
            eNone = 0,             // No compression
        };


        // Default copy constructor and assignment operators ok.
    private:
        static IAP* _sOnly;

        Image<RGB> _readRGB  (FILE* fp, const IAPImageHeader& imageHeader);
        Image<Pel8>  _readPel8 (FILE* fp, const IAPImageHeader& imageHeader);
        // Internal functions to read various image types

        IAP ();
        ~IAP ();
    };


    // This object will make sure that jpeg is added to our supported list
    // during static initialization

    class IAP_installer
    {
    public:
        static IAP_installer installer;
    private:
        IAP_installer ();
    };

}
#endif // _iap_h_
